package addEdges;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import removeDanglingPage.ReadBytes;

import mapred.AbstractReducer;
import mapred.util.Bytes;

public class AddEdgesMerge_Reducer extends AbstractReducer<BytesWritable, BytesWritable, BytesWritable, BytesWritable>{

	@Override
	public void reduce(BytesWritable key, Iterator<BytesWritable> values,
			OutputCollector<BytesWritable, BytesWritable> output,
			Reporter reporter) throws IOException {
		// TODO Auto-generated method stub
		double pagerank = 0d;
		while(values.hasNext()) {
			ReadBytes v = new ReadBytes(values.next());
			double p = v.nextDouble();
			pagerank += p;
		}
		
		output.collect(key, new BytesWritable(Bytes.Double2Bytes(pagerank)));
		
	}

//	@Override
//	public void reduce(Text key, Iterator<Text> values,
//			OutputCollector<Text, Text> output, Reporter reporter)
//			throws IOException {
//		// TODO Auto-generated method stub
//
//            double pagerank = 0d;
//            while(values.hasNext()) {
//                String v = values.next().toString().trim();
//                double p = Double.parseDouble(v);
//                pagerank += p;
//            }
//            output.collect(key, new Text(String.valueOf(pagerank)));
//		
//	}

}
